home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / fault / RCS / fault.c,v < prev   
Encoding:
Text File  |  1992-07-17  |  4.9 KB  |  265 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.3 srv027:1.3 srv026:1.3 srv024:1.3 srv021:1.3 srv018:1.3 srv014:1.3 srv010:1.3 srv008:1.3 srv007:1.3 srv006:1.3 srv004:1.3;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     92.03.12.20.47.29;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     92.02.28.19.50.16;  author kupfer;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     91.11.14.20.28.18;  author kupfer;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @Quick test program to cause an address fault.
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @Add support for testing signal handlers.
  33. @
  34. text
  35. @/* 
  36.  * quick test program to cause an address fault. 
  37.  * 
  38.  * $Header$
  39.  */
  40.  
  41. /* 
  42.  * usage: fault [ stack|readonly|ignore|handle ] 
  43.  * 
  44.  * where "stack" means to touch enough pages to test the stack-extension 
  45.  * code in Sprite, "readonly" means to try writing read-only memory, and 
  46.  * "ignore" means to disable receipt of the signal.  Only one option can be 
  47.  * specified. 
  48.  * 
  49.  * Should probably not be built with optimization turned on.
  50.  */
  51.  
  52. #include <sprite.h>
  53. #include <mach.h>
  54. #include <setjmp.h>
  55. #include <signal.h>
  56. #include <status.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <test.h>
  61. #include <vm.h>
  62.  
  63. jmp_buf jmpBuf;            /* state saved by setjmp */
  64. int numFaults = 0;        /* number of times we caused an exception */
  65. char globalCh;            /* sometimes used by signal handler */
  66.  
  67. void DoStack();
  68. void DoReadOnly();
  69. void SegvHandler(), FpeHandler();
  70.  
  71. int
  72. main(argc, argv)
  73.     int argc;
  74.     char *argv[];
  75. {
  76.     char *bogusPtr = (char *)0x23456789; /* 0x10000000 is heap on DECstation */
  77.     char ch;
  78.  
  79.     if (argc > 1) {
  80.     if (strcmp(argv[1], "stack") == 0) {
  81.         (void)DoStack((int)vm_page_size * 2);
  82.         exit(0);
  83.     } else if (strcmp(argv[1], "readonly") == 0) {
  84.         DoReadOnly();
  85.         exit(0);
  86.     } else if (strcmp(argv[1], "ignore") == 0) {
  87.         if (signal(SIGSEGV, SIG_IGN) == BADSIG) {
  88.         perror("can't ignore SIGSEGV");    /* XXX not allowed by Sprite */
  89.         exit(1);
  90.         }
  91.     } else if (strcmp(argv[1], "handle") == 0) {
  92.         if (signal(SIGSEGV, SegvHandler) == BADSIG) {
  93.         perror("Can't register SIGSEGV handler");
  94.         exit(1);
  95.         }
  96.         if (signal(SIGFPE, FpeHandler) == BADSIG) {
  97.         perror("Can't register SIGFPE handler");
  98.         exit(1);
  99.         }
  100.     }
  101.     }
  102.  
  103.     if (setjmp(jmpBuf)) {
  104.     Test_PutMessage("longjmp\n");
  105.     }
  106.     if (numFaults > 5) {
  107.     exit(numFaults);
  108.     }
  109.  
  110.     ch = *bogusPtr;
  111.     Test_PutMessage("it worked?\n");
  112.     return ch;            /* prevent optimizer from interfering */
  113. }
  114.  
  115. /* 
  116.  * Recurse a bunch of times to force automatic growing of the stack. 
  117.  */
  118. void
  119. DoStack(numTimes)
  120.     int numTimes;        /* number of times to recurse */
  121. {
  122.     int foo;            /* force something to go on the stack */
  123.  
  124.     foo = numTimes;
  125.     if (numTimes > 0) {
  126.     (void)DoStack(numTimes - 1);
  127.     }
  128. #ifdef lint
  129.     foo = foo;
  130. #endif
  131. }
  132.  
  133. /* 
  134.  * Map a file read-only then try to write to it.  The if'd out code has 
  135.  * interesting addressing errors you can get (some only happen on certain 
  136.  * machine types).
  137.  */
  138. void
  139. DoReadOnly()
  140. {
  141.     ReturnStatus status;
  142.     Address startAddr;
  143.     char *fileName = "testInput";
  144.     int foo = 0;
  145.  
  146.     status = Vm_MapFile(fileName, TRUE, (off_t)0, 1, &startAddr);
  147.     if (status != SUCCESS) {
  148.     Test_PutMessage("Couldn't map `");
  149.     Test_PutMessage(fileName);
  150.     Test_PutMessage("': ");
  151.     Test_PutMessage(Stat_GetMsg(status));
  152.     Test_PutMessage("\n");
  153.     return;
  154.     }
  155.  
  156. #if 0
  157.     foo = *(int *)(startAddr + 1); /* force word reference to unaligned addr */
  158.     *(int *)(startAddr+1) = 42;    /* ditto */
  159. #endif
  160.     *(int *)startAddr = 42;
  161.  
  162. #ifdef lint
  163.     foo = foo;
  164. #endif
  165. }
  166.  
  167. void
  168. SegvHandler(sigNum, code, contextPtr, addr)
  169.     int sigNum, code;
  170.     Address contextPtr, addr;
  171. {
  172. #if 0
  173.     printf("(%d, %d, 0x%x, 0x%x)\n", sigNum, code, contextPtr, addr);
  174.     exit(0);
  175. #endif
  176. #if 0
  177.     ++numFaults;
  178.     longjmp(jmpBuf, 1);
  179. #endif
  180. #if 0
  181.     /* Cause another instance of the same exception. */
  182.     globalCh = *(char *)-1;
  183.     Test_PutMessage("SegvHandler: shouldn't get here.\n");
  184.     exit(1);
  185. #endif
  186.     /* Cause a different exception. */
  187.     double foo, bar;
  188.  
  189.     foo = 0.0;
  190.     bar = 0.0;
  191.     printf("%f\n", foo/bar);
  192.     Test_PutMessage("SegvHandler: shouldn't get here.\n");
  193.     exit(1);
  194.  
  195. #ifdef lint
  196.     sigNum = sigNum;
  197.     code = code;
  198.     contextPtr = contextPtr;
  199.     addr = addr;
  200. #endif
  201. }
  202.  
  203. void
  204. FpeHandler()
  205. {
  206.     ++numFaults;
  207.     longjmp(jmpBuf, 1);
  208. }
  209. @
  210.  
  211.  
  212. 1.2
  213. log
  214. @Add "ignore" option to block receipt of the SIGSEGV.  Lint.
  215. @
  216. text
  217. @d1 5
  218. a5 1
  219. /* quick test program to cause an address fault. */
  220. d8 1
  221. a8 1
  222.  * usage: fault [ stack|readonly|ignore ] 
  223. d20 1
  224. d29 4
  225. d35 1
  226. d57 9
  227. d69 7
  228. d131 43
  229. @
  230.  
  231.  
  232. 1.1
  233. log
  234. @Initial revision
  235. @
  236. text
  237. @d4 1
  238. a4 1
  239.  * usage: fault [ stack|readonly ] 
  240. d7 3
  241. a9 1
  242.  * code in Sprite.
  243. d16 1
  244. d18 2
  245. d24 3
  246. d32 1
  247. a32 1
  248.     char *bogusPtr = (char *)0x12345678;
  249. d42 5
  250. d51 2
  251. a52 1
  252.     ch = ch;            /* lint */
  253. d58 1
  254. d68 3
  255. a70 1
  256.     return foo;
  257. d78 1
  258. d86 1
  259. a86 1
  260.     status = Vm_MapFile(fileName, TRUE, 0, 1, &startAddr);
  261. d102 3
  262. a104 1
  263.     return foo;
  264. @
  265.